home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / basics / glwidget.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.5 KB  |  298 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *
  19.  *    glwidget.c - an example of GL widget used with the Motif toolkit.
  20.  *
  21.  *  glwidget.c -
  22.  *
  23.  *  Ivan Hajadi & Sam Chen
  24.  *  1-April-1992
  25.  *
  26.  *  to compile:
  27.  *  cc -o glwidget glwidget.c -lXirisw -lgl_s -lXm -lXt -lX11_s -lPW -lsun 
  28.  */
  29.  
  30. /* Motif includes */
  31. #include <Xm/Xm.h>
  32. #include <Xm/Form.h>
  33. #include <Xm/PushB.h>
  34.  
  35. /* GL widget include */
  36. #include <X11/Xirisw/GlxMDraw.h>
  37.  
  38. int angle_x = 0, angle_y = 0;
  39. void drawscene(int, int);
  40.  
  41. GLXconfig rgb_mode[] = {
  42.     { GLX_NORMAL, GLX_RGB, TRUE },
  43.     { GLX_NORMAL, GLX_DOUBLE, TRUE },
  44.     { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  45.     { 0,          0,       0,   }
  46. };
  47.  
  48. main(int argc, char** argv)
  49. {
  50.     Widget top, form, pushb;
  51.     Widget gl_widget;
  52.  
  53.     /* Prototypes */
  54.     void installColormap(Widget toplevel, Widget glw);
  55.     void quitCB(Widget, XtPointer, XtPointer);
  56.     void ginitCB(Widget, XtPointer, XtPointer);
  57.     void exposeCB(Widget, XtPointer, XtPointer);
  58.     void inputCB(Widget, XtPointer, XtPointer);
  59.  
  60.  
  61.     XtAppContext app_context;
  62.     Arg args[20];
  63.     int n;
  64.     
  65.     top = XtAppInitialize(&app_context, "Cube", NULL, 0, 
  66.          &argc, argv, NULL, NULL, 0
  67.     );
  68.     n=0;
  69.     XtSetArg(args[n], XmNwidth, 730); n++;
  70.     XtSetArg(args[n], XmNheight, 445); n++;
  71.     form = XmCreateForm(top, "form", args, n);
  72.     XtManageChild(form);
  73.  
  74.     /* Create GL widget */
  75.  
  76.     n = 0;
  77.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  78.     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  79.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;  
  80.     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  81.     XtSetArg(args[n], XmNleftOffset, 20); n++;
  82.     XtSetArg(args[n], XmNrightOffset, 100); n++;
  83.     XtSetArg(args[n], XmNbottomOffset, 40); n++;
  84.     XtSetArg(args[n], XmNtopOffset, 40); n++;
  85.     XtSetArg(args[n], XmNwidth, 500); n++;
  86.     XtSetArg(args[n], XmNheight, 400); n++;
  87.     XtSetArg(args[n], GlxNglxConfig, rgb_mode); n++;
  88.     gl_widget = GlxCreateMDraw(form, "gl_widget", args, n);
  89.     XtManageChild(gl_widget);
  90.  
  91.     /* Add callbacks */
  92.  
  93.     XtAddCallback(gl_widget, GlxNginitCallback, ginitCB, NULL);
  94.     XtAddCallback(gl_widget, GlxNexposeCallback, exposeCB, NULL);
  95.     XtAddCallback(gl_widget, GlxNinputCallback, inputCB, NULL); 
  96.  
  97.     n=0;
  98.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  99.     XtSetArg(args[n], XmNrightOffset, 10); n++;
  100.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  101.     XtSetArg(args[n], XmNbottomOffset, 10); n++;
  102.     XtSetArg(args[n], XmNtraversalOn, False); n++;
  103.     pushb = XmCreatePushButton(form, "Quit", args, n);
  104.     XtManageChild(pushb);
  105.  
  106.     XtAddCallback(pushb, XmNactivateCallback, quitCB, NULL);
  107.  
  108.     XtRealizeWidget(top);
  109.     installColormap(top, gl_widget);
  110.     XtAppMainLoop(app_context);
  111. }
  112.  
  113. /*
  114.  * This routine will install a particular gl widgets's colormap onto the
  115.  * top level window.  It may not be called until after the windows have
  116.  * been realized.
  117.  */
  118. void installColormap(Widget toplevel, Widget glw)
  119. {
  120.     Window windows[2];
  121.  
  122.     windows[0] = XtWindow(glw);
  123.     windows[1] = XtWindow(toplevel);
  124.     XSetWMColormapWindows(XtDisplay(toplevel), XtWindow(toplevel), windows, 2);
  125. }
  126.  
  127. void inputCB(Widget w, XtPointer clientD, XtPointer callD)
  128. {
  129.     GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
  130.     static int old_x=0, old_y=0, x, y;
  131.  
  132.     switch(call_data->event->type) {
  133.     case MotionNotify:
  134.         x = call_data->event->xmotion.x;
  135.         y = call_data->event->xmotion.y;
  136.  
  137.         if (x > old_x + 2)
  138.            angle_y += 75;
  139.  
  140.         if (x < old_x - 2)
  141.            angle_y -= 75;
  142.  
  143.         if (y > old_y + 2)
  144.            angle_x += 75;
  145.  
  146.         if (y < old_y - 2)
  147.            angle_x -= 75;
  148.  
  149.         if ((angle_y >= 3600) || (angle_y <= -3600))
  150.            angle_y = 0;
  151.         if ((angle_x >= 3600) || (angle_x <= -3600))
  152.            angle_x = 0;
  153.  
  154.         old_x = x;  old_y = y;
  155.  
  156.         drawscene(angle_x, angle_y);
  157.         break;
  158.  
  159.     default:
  160.         break;
  161.     }  /* switch */
  162. }
  163.  
  164. void ginitCB(Widget w, XtPointer clientD, XtPointer callD)
  165. {
  166.     GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
  167.  
  168.     /* listen for pointer motion events */
  169.     XSelectInput(
  170.         XtDisplay(w), XtWindow(w), XtBuildEventMask(w) | PointerMotionMask
  171.     );
  172.  
  173.     /* establish what to do with these events (call the gl input callback) */
  174.     XtAugmentTranslations(
  175.         w, XtParseTranslationTable("<MotionNotify>: glxInput()")
  176.     );
  177.  
  178.     GLXwinset(XtDisplay(w), call_data->window);
  179.  
  180.     shademodel(GOURAUD);
  181.     zbuffer(TRUE);
  182.     subpixel(TRUE);
  183.     lsetdepth(getgdesc(GD_ZMIN), getgdesc(GD_ZMAX));
  184.     mmode(MVIEWING);
  185.     perspective(450, 1, 1, 100);
  186. }
  187.  
  188. void exposeCB(Widget w, XtPointer clientD, XtPointer callD)
  189. {
  190.     GLXwinset (XtDisplay(w), XtWindow(w));
  191.     drawscene(angle_x, angle_y);
  192. }
  193.  
  194. void quitCB(Widget w, XtPointer clientD, XtPointer callD)
  195. {
  196.     exit(0);
  197. }
  198.  
  199. void drawscene(int angle_x, int angle_y)
  200. {
  201.     /* Draw smooth-shaded cube */
  202.     static long v1[4][3] = {
  203.         {-10, -10, 10},
  204.         {10, -10, 10},
  205.         {10, 10, 10},
  206.         {-10, 10, 10},
  207.     };
  208.     static long v2[4][3] = {
  209.         {-10, -10, -10},
  210.         {-10, 10, -10},
  211.         {10, 10, -10},
  212.         {10, -10, -10},
  213.     };
  214.     static long colors1[] = {
  215.         0x000000FF,
  216.         0x00FF0000,
  217.         0x0000FF00,
  218.         0x00FFFF00,
  219.     };
  220.     static long colors2[] = {
  221.         0x0000FFFF,
  222.         0x00FF00FF,
  223.         0x00FFFFFF,
  224.         0x0FF00000,
  225.     };
  226.     register int i;
  227.  
  228.     reshapeviewport();
  229.     czclear(0, getgdesc(GD_ZMAX));
  230.     pushmatrix();
  231.     polarview(80, 0, 250, 0);
  232.     
  233.     pushmatrix();
  234.     rotate(angle_x, 'x');
  235.     rotate(angle_y, 'y');
  236.  
  237.     bgnpolygon();
  238.         for(i=0; i<4; i++) {
  239.             cpack(colors1[i]);
  240.             v3i(v1[i]);
  241.         }
  242.     endpolygon();
  243.     bgnpolygon();
  244.         for(i=0; i<4; i++) {
  245.             cpack(colors2[i]);
  246.             v3i(v2[i]);
  247.         }
  248.     endpolygon();
  249.  
  250.     bgnpolygon();
  251.         cpack(colors1[1]);
  252.         v3i(v1[1]);
  253.         cpack(colors1[2]);
  254.         v3i(v1[2]);
  255.         cpack(colors2[2]);
  256.         v3i(v2[2]);
  257.         cpack(colors2[3]);
  258.         v3i(v2[3]);
  259.     endpolygon();
  260.     bgnpolygon();
  261.         cpack(colors1[0]);
  262.         v3i(v1[0]);
  263.         cpack(colors1[3]);
  264.         v3i(v1[3]);
  265.         cpack(colors2[1]);
  266.         v3i(v2[1]);
  267.         cpack(colors2[0]);
  268.         v3i(v2[0]);
  269.     endpolygon();
  270.  
  271.     bgnpolygon();
  272.         cpack(colors1[2]);
  273.         v3i(v1[2]);
  274.         cpack(colors1[3]);
  275.         v3i(v1[3]);
  276.         cpack(colors2[1]);
  277.         v3i(v2[1]);
  278.         cpack(colors2[2]);
  279.         v3i(v2[2]);
  280.     endpolygon();
  281.     bgnpolygon();
  282.         cpack(colors1[1]);
  283.         v3i(v1[1]);
  284.         cpack(colors1[0]);
  285.         v3i(v1[0]);
  286.         cpack(colors2[0]);
  287.         v3i(v2[0]);
  288.         cpack(colors2[3]);
  289.         v3i(v2[3]);
  290.     endpolygon();
  291.  
  292.     popmatrix();
  293.     popmatrix();
  294.  
  295.     swapbuffers();
  296. }
  297.  
  298.